package com.atgui.yygh.controller.api;

import com.atgui.commom.helper.HttpRequestHelper;
import com.atgui.commom.utils.MD5;
import com.atgui.yygh.model.hosp.Department;
import com.atgui.yygh.model.hosp.Hospital;
import com.atgui.yygh.model.hosp.Schedule;
import com.atgui.yygh.service.DepartmentService;
import com.atgui.yygh.service.HospitalService;
import com.atgui.yygh.service.HospitalSetService;
import com.atgui.yygh.service.ScheduleService;
import com.atgui.yygh.vo.hosp.DepartmentQueryVo;
import com.atgui.yygh.vo.hosp.ScheduleQueryVo;
import com.atguigu.yygh.exception.YyghException;
import com.atguigu.yygh.result.Result;
import com.atguigu.yygh.result.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.Map;

/**
 * zz: wzh
 * time 2022 08 21 17 13
 **/
@RequestMapping("/api/hosp")
@RestController
public class ApiController {

    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;
    //删除医院
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hoscode = (String)paramMap.get("hoscode");
        String hosScheduleId=(String) paramMap.get("hosScheduleId");
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }
    //查询医院
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //这里签名效验跳过
        String hoscode = (String)paramMap.get("hoscode");
        //科室编号
        String depcode= (String)paramMap.get("depcode");
        int page=StringUtils.isEmpty(paramMap.get("page"))?1:Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));

        ScheduleQueryVo scheduleQueryVo=new ScheduleQueryVo() ;
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);

        Page<Schedule> schedulePage= scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(schedulePage);
    }
    //上传排班接口
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //这里签名效验跳过
        scheduleService.save(paramMap);
        return Result.ok();
    }
    //删除科室
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");

        //1 获取医院系统传递过来的签名,签名进行MD5加密
        String hospSign = (String)paramMap.get("sign");

        //2 根据传递过来医院编码，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3 把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //4 判断签名是否一致
        if(!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }
    //查询医院科室
    @PostMapping("department/list")
    public Result showDepartment(HttpServletRequest request){
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hoscode = (String)paramMap.get("hoscode");
        int page=StringUtils.isEmpty(paramMap.get("page"))?1:Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));

//        //1 获取医院系统传递过来的签名,签名进行MD5加密
//        String hospSign = (String)paramMap.get("sign");
//
//        //2 根据传递过来医院编码，查询数据库，查询签名
//        String signKey = hospitalSetService.getSignKey(hoscode);
//
//        //3 把数据库查询签名进行MD5加密
//        String signKeyMd5 = MD5.encrypt(signKey);
//
//        //4 判断签名是否一致
//        if(!hospSign.equals(signKeyMd5)) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }
        DepartmentQueryVo departmentQueryVo=new DepartmentQueryVo() ;
        departmentQueryVo.setHoscode(hoscode);

        Page<Department> departmentList= departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(departmentList);
    }
    //保存科室
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院编号
        String hoscode = (String)paramMap.get("hoscode");
        //1 获取医院系统传递过来的签名,签名进行MD5加密
        String hospSign = (String)paramMap.get("sign");

        //2 根据传递过来医院编码，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3 把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //4 判断签名是否一致
        if(!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.save(paramMap);
        return Result.ok();
    }
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        //获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院编号
        String hoscode = (String)paramMap.get("hoscode");
        //1 获取医院系统传递过来的签名,签名进行MD5加密
        String hospSign = (String)paramMap.get("sign");

        //2 根据传递过来医院编码，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3 把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //4 判断签名是否一致
        if(!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法实现根据医院编号查询
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request){
        //获取医院传递过来的信息
        Map<String, String[]> requestParameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(requestParameterMap);
        //获取医院系统那里得出来的sign
        String sign = (String) map.get("sign");
        String hascode = (String) map.get("hoscode");
        //冲数据库中查到的sign
        String signKey =hospitalSetService.getSignKey(hascode);
        String encrypt = MD5.encrypt(signKey);
        //判断签名是否一致
        if (!encrypt.equals(sign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //在传输过程中+会变成""
        String logoData=(String) map.get("logoData");
        logoData=logoData.replaceAll(" ","+");
        map.put("logoData",logoData);
        hospitalService.save(map);
        return Result.ok();
    }
}
